package com.example.nftapp.frg.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.nftapp.R;
import com.example.nftapp.VideoPlayerActivity;
import com.example.nftapp.adapter.BannerAdapter;
import com.example.nftapp.adapter.CommonPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment_TJ extends Fragment {

    private final List<Fragment> fragments = new ArrayList<>();
    private final List<String> tabs = new ArrayList<>();
    private final List<Integer> bannerData = new ArrayList<>();
    //Handler+Runnable实现自动轮播
    private Handler handler;
    private ViewPager2 vpBanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 准备数据
        tabs.add("热门推荐");
        tabs.add("发行日历");
        fragments.add(new TuiJianFragment_RM());
        fragments.add(new TuiJianFragment_RL());
        // 准备轮播图数据
        bannerData.add(R.mipmap.img_home_banner1);
        bannerData.add(R.mipmap.img_home_banner1);
        bannerData.add(R.mipmap.img_home_banner1);
        bannerData.add(R.mipmap.img_home_banner1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tj, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TabLayout tabBanner = view.findViewById(R.id.tabBanner);
        vpBanner = view.findViewById(R.id.vpBanner);
        vpBanner.setAdapter(new BannerAdapter(bannerData, new BannerAdapter.OnBannerItemClickListener() {
            @Override
            public void onClick(int position) {
                // 实现轮播图点击事件，跳转至视频播放界面VideoPlayerActivity
                startActivity(new Intent(getContext(), VideoPlayerActivity.class));
            }
        }));
        new TabLayoutMediator(tabBanner, vpBanner, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                ImageView imageView = (ImageView) LayoutInflater.from(requireActivity()).inflate(R.layout.layout_custom_tab_banner, null);
                tab.setCustomView(imageView);
                if (position == 0) {
                    imageView.setImageResource(R.drawable.bg_dot_selected);
                }
            }
        }).attach();
        tabBanner.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ImageView imageView = (ImageView) tab.getCustomView();
                assert imageView != null;
                imageView.setImageResource(R.drawable.bg_dot_selected);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView imageView = (ImageView) tab.getCustomView();
                assert imageView != null;
                imageView.setImageResource(R.drawable.bg_dot_unselected);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        startAutoScroll();

        // 初始化TabLayout+ViewPager2组件
        TabLayout tabsHomeTJ = view.findViewById(R.id.tabsHomeTJ);
        ViewPager2 vpHomeTJ = view.findViewById(R.id.vpHomeTJ);
        // 通过PagerAdapter将Fragment与ViewPager2绑定
        vpHomeTJ.setAdapter(new CommonPagerAdapter(requireActivity(), fragments));
        // 将ViewPager2与TabLayout绑定
        new TabLayoutMediator(tabsHomeTJ, vpHomeTJ, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabs.get(position));
            }
        }).attach();
    }

    /**
     * 开启自动轮播
     */
    private void startAutoScroll() {
        handler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                vpBanner.setCurrentItem((vpBanner.getCurrentItem() + 1) % bannerData.size(), true);
                handler.postDelayed(this, 3000);
            }
        };
        handler.postDelayed(runnable, 3000);
    }

}